/**
 * Created by lwy on 2017/5/28.
 */

var phone = getItem('phone');
//发送验证码
$('.code-button').unbind('click').bind('click',function () {
    sendCode();
})

$('.code-button2').unbind('click').bind('click',function () {
    sendCode2();
})
function sendCode(){
    if(!ifCyber()){
        mui.toast('请检查网络重试');
    }

    verify();
}

function verify() {
    $(".code-button").html("发送中…");
    $(".get-code").unbind();
    $.ajax({
        url:'/user/sendMobileCode_Vtoken.json',
        data:{
            platform:'wap',
            token:getItem('token'),
        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                //$(".get-code").bind('click',sendCode);
                return;
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })
}

document.getElementById('code_input').oninput=function () {
    if($(this).val().length==5){
        $.ajax({
            //验证码是否正确接口
            url:'/imageVerifyC/verifyCode.json',
            dataType:'json',
            data:{
                mobile:$('#phone').val(),
                validateCode:$('#code_input').val(),
            },
            type:'post',
            success:function (data) {
                if(data.code!=1){
                    mui.toast(data.msg);
                    return;
                }
                if(data.code==1){
                    if(data.verifyCode){
                        $('.zhezhao').hide();
                        twice=true;
                        verifyCode = $('#code_input').val();
                        setItem('verifyCode',$('#code_input').val());
                        verify2();
                    }else{
                        mui.toast('验证码错误');
                        var timestamp = (new Date()).valueOf();
                        $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                        return;
                    }
                }
            }
        })
    }
}

//关闭验证窗口
$('#close').click(function () {
    $('.zhezhao').hide();
})

function sendCode2(){
    if(!$('#phone').val().match(/^1[2-9][0-9]{9}$/)){
        mui.toast('请输入正确手机号码');
        return;
    }

    //验证该手机号是否发送过验证码
    $.ajax({
        url:'/imageVerifyC/ifCode.json',
        dataType:'json',
        data:{
            mobile:$('#phone').val(),
        },
        type:'post',
        success:function (data) {
            if(data.ifCode){
                //如果为true，从本地获取图片验证码
                verifyCode = getItem('verifyCode');
                verify2();
            }else{
                //如果为false，需要输入
                var timestamp = (new Date()).valueOf();
                $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                $('#code_input').val('');
                $('.zhezhao').show();
            }
        }
    })
}

function verify2(){
    $(".code-button2").html("发送中…");
    $(".get-code2").unbind();
    $.ajax({
        url:'/user/sendMobileCode_ValidateCode.json',
        data:{
            mobile:$('#phone').val(),
            platform:'wap',
            validateCode:verifyCode,

        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                if(data.msg=='图形验证码验证失败'){
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val());
                    $('#code_input').val('');
                    $('.zhezhao').show();
                    return;
                }
                mui.toast(data.msg);
                $(".code-button2").html("重新发送");
                $(".get-code2").bind('click',sendCode);
                return;
            }
            timeUpdate2.init($(".code-button2"), 60);

        }
    })
}

var timeUpdate = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};

var timeUpdate2 = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode2();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};

//修改
$(function(){
    $('.set-certain').click(function(){
        if(!ifCyber()){
            mui.toast('请检查网络重试');
        }
                    $.ajax({
                        url:'/user/checkTwoMobileCode.json',
                        dataType:'json',
                        data:{
                            mobileOld:phone,
                            token:getItem('token'),
                            mobileNew:$('#phone').val(),
                            code1:$('.code-input').val(),
                            code2:$('.code-input2').val(),
                            platform:'wap',
                        },
                        success:function(data){
                            if(data.code!=1){
                                mui.toast(data.msg);
                                return;
                            };
                            if(data.code==1){
                                removeItem('token');
                                removeItem('phone');
                                removeItem('passWord');
                                window.location.href="login.html"
                            }
                        }
                    })
    })
})
